package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Order;
import com.ruoyi.system.service.IOrderService;
import com.ruoyi.system.service.impl.OrderServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ProviderClaim;
import com.ruoyi.system.service.IProviderClaimService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.util.MapperUtils;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2025-03-03
 */
@RestController
@RequestMapping("/system/claim")
public class ProviderClaimController extends BaseController
{
    private static final Logger logger = LoggerFactory.getLogger(ProviderClaimController.class);

    @Autowired
    private IProviderClaimService providerClaimService;
    @Autowired
    private IOrderService orderService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:claim:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProviderClaim providerClaim)
    {
        startPage();
        List<ProviderClaim> list = providerClaimService.selectProviderClaimList(providerClaim);
        return getDataTable(list);
    }

    @GetMapping("/providerClaimList")
    public TableDataInfo providerClaimList(ProviderClaim providerClaim)
    {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        SysDept sysdept = sysUser.getDept();
        providerClaim.setProviderName(sysdept.getDeptName());
        providerClaim.setZxkStatus("1");
        startPage();
        List<ProviderClaim> list = providerClaimService.selectProviderClaimList(providerClaim);
        return getDataTable(list);
    }

    @GetMapping("/wzgsClaimList")
    public TableDataInfo wzgsClaimList(ProviderClaim providerClaim)
    {
        /*SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        SysDept sysdept = sysUser.getDept();
        providerClaim.setProviderName(sysdept.getDeptName());
        providerClaim.setZxkStatus("1");*/
        startPage();
        List<ProviderClaim> list = providerClaimService.selectWzgsClaimList(providerClaim);
        return getDataTable(list);
    }

    @PostMapping("/claimList")
    public AjaxResult claimList(ProviderClaim providerClaim,@RequestParam("pageNum")Integer pageNum,@RequestParam("pageSize")Integer pageSize)
    {
        Map<String, Object> result = new HashMap<>();

        try{
            List<ProviderClaim> providerClaimlist = providerClaimService.selectProviderClaimList(providerClaim);
            // 对列表进行分页处理
            int total = providerClaimlist.size();
            int startIndex = (pageNum - 1) * pageSize;
            int endIndex = Math.min(startIndex + pageSize, total);
            List<ProviderClaim> paginatedList = providerClaimlist.subList(startIndex, endIndex);
            // 返回分页结果
            result.put("list", paginatedList);
            result.put("total", total);
            result.put("pageNum", pageNum);
            result.put("pageSize", pageSize);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("对不起！系统错误请稍后再试");
        }

        return AjaxResult.success(result);
    }


    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:claim:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProviderClaim providerClaim)
    {
        List<ProviderClaim> list = providerClaimService.selectProviderClaimList(providerClaim);
        ExcelUtil<ProviderClaim> util = new ExcelUtil<ProviderClaim>(ProviderClaim.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:claim:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(providerClaimService.selectProviderClaimById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:claim:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProviderClaim providerClaim)
    {
        return toAjax(providerClaimService.insertProviderClaim(providerClaim));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:claim:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProviderClaim providerClaim)
    {
        return toAjax(providerClaimService.updateProviderClaim(providerClaim));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:claim:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(providerClaimService.deleteProviderClaimByIds(ids));
    }

    @RequestMapping("searchProviderClaim")
    public AjaxResult searchProviderClaim(@RequestParam("sendOrderId")String sendOrderId){
        Map<String, Object> result = new HashMap<>();
        try
        {

        }catch (Exception e)
        {
            e.printStackTrace();
            return AjaxResult.error("对不起！系统错误请稍后再试");
        }
        return AjaxResult.success(result);
    }

    @Anonymous
    @RequestMapping("/insertClaim")
    @Transactional(rollbackFor = Exception.class)
    public String insertOrder(@RequestBody List<ProviderClaim> claimList) throws Exception {
        HashMap<String,Object> returnMap = new HashMap<>();
        try{
            for (ProviderClaim providerClaim:claimList)
            {
                ProviderClaim providerClaims = providerClaimService.selectProviderClaimByClaimId(providerClaim);
                if(providerClaims==null)
                {
                    int count = providerClaimService.insertProviderClaim(providerClaim);
                }
                else
                {
                    providerClaimService.updateProviderClaimByClaimId(providerClaim);
                }
            }
            // int count = providerClaimService.insertClaimBitchs(claimList);
            returnMap.put("code",200);
            returnMap.put("msg","推送订单成功");
        }catch (Exception e){
            returnMap.put("code",500);
            returnMap.put("msg",e);
        }
        return MapperUtils.mapToJson(returnMap);
    }
    @Anonymous
    @RequestMapping("/updateClaimListForZXK")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/claimList")
    public String updateClaimListForZXK(@RequestBody List<ProviderClaim> claimList) throws Exception {
        HashMap<String,Object> returnMap = new HashMap<>();
        try{
            String createTime = DateUtils.getTime();
            for (ProviderClaim providerClaim:claimList)
            {
                providerClaim.setCreateTime(createTime);
                int count = providerClaimService.updateClaimListForZXK(providerClaim);
                if(count>0)
                {
                    returnMap.put("code",200);
                    returnMap.put("msg","索赔产品已确认回收");
                }
                else
                {
                    returnMap.put("code",500);
                    returnMap.put("msg","保存失败！");
                }
            }
        }catch (Exception e){
            returnMap.put("code",500);
            returnMap.put("msg",e);
        }
        return MapperUtils.mapToJson(returnMap);
    }

    @Anonymous
    @RequestMapping("/updateClaimListForProvider")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/claimList")
    public String updateClaimListForProvider(@RequestBody List<ProviderClaim> claimList) throws Exception {
        HashMap<String,Object> returnMap = new HashMap<>();
        try{
            String createTime = DateUtils.getTime();
            for (ProviderClaim providerClaim:claimList)
            {
                providerClaim.setCreateTime(createTime);
                int count = providerClaimService.updateClaimListForProvider(providerClaim);
                if(count>0)
                {
                    returnMap.put("code",200);
                    returnMap.put("msg","索赔产品已确认赔付");
                }
                else
                {
                    returnMap.put("code",500);
                    returnMap.put("msg","保存失败！");
                }
            }
        }catch (Exception e){
            returnMap.put("code",500);
            returnMap.put("msg",e);
        }
        return MapperUtils.mapToJson(returnMap);
    }

    @Anonymous
    @RequestMapping("/updateClaimListForWzgs")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/claimList")
    public String updateClaimListForWzgs(@RequestBody List<ProviderClaim> claimList) throws Exception {
        HashMap<String,Object> returnMap = new HashMap<>();
        try{
            String createTime = DateUtils.getTime();
            for (ProviderClaim providerClaim:claimList)
            {
                providerClaim.setCreateTime(createTime);
                int count = providerClaimService.updateClaimListForWzgs(providerClaim);
                if(count>0)
                {
                    returnMap.put("code",200);
                    returnMap.put("msg","索赔产品已确认不赔付");
                }
                else
                {
                    returnMap.put("code",500);
                    returnMap.put("msg","保存失败！");
                }
            }
        }catch (Exception e){
            returnMap.put("code",500);
            returnMap.put("msg",e);
        }
        return MapperUtils.mapToJson(returnMap);
    }

    @Anonymous
    @RequestMapping("/cancelClaimListForProvider")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/claimList")
    public String cancelClaimListForProvider(@RequestBody List<ProviderClaim> claimList) throws Exception {
        HashMap<String,Object> returnMap = new HashMap<>();
        try{
            String createTime = DateUtils.getTime();
            for (ProviderClaim providerClaim:claimList)
            {
                providerClaim.setCreateTime(createTime);
                int count = providerClaimService.cancelClaimListForProvider(providerClaim);
                if(count>0)
                {
                    returnMap.put("code",200);
                    returnMap.put("msg","索赔产品拒绝赔付");
                }
                else
                {
                    returnMap.put("code",500);
                    returnMap.put("msg","保存失败！");
                }
            }
        }catch (Exception e){
            returnMap.put("code",500);
            returnMap.put("msg",e);
        }
        return MapperUtils.mapToJson(returnMap);
    }

    @Anonymous
    @RequestMapping("/cancelClaimListForWzgs")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/claimList")
    public String cancelClaimListForWzgs(@RequestBody List<ProviderClaim> claimList) throws Exception {
        HashMap<String,Object> returnMap = new HashMap<>();
        try{
            String createTime = DateUtils.getTime();
            for (ProviderClaim providerClaim:claimList)
            {
                providerClaim.setCreateTime(createTime);
                int count = providerClaimService.cancelClaimListForWzgs(providerClaim);
                if(count>0)
                {
                    returnMap.put("code",200);
                    returnMap.put("msg","索赔产品拒绝赔付");
                }
                else
                {
                    returnMap.put("code",500);
                    returnMap.put("msg","保存失败！");
                }
            }
        }catch (Exception e){
            returnMap.put("code",500);
            returnMap.put("msg",e);
        }
        return MapperUtils.mapToJson(returnMap);
    }
    @Anonymous
    @RequestMapping("/unpackClaimListForZXK")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/claimList")
    public String unpackClaimListForZXK(@RequestBody List<ProviderClaim> claimList) throws Exception {
        HashMap<String,Object> returnMap = new HashMap<>();
        try{
            String createTime = DateUtils.getTime();
            for (ProviderClaim providerClaim:claimList)
            {
               /* providerClaim.setCreateTime(createTime);
                int count = providerClaimService.unpackClaimListForZXK(providerClaim);
                if(count>0)
                {
                    returnMap.put("code",200);
                    returnMap.put("msg","拆单完成！");
                }
                else
                {
                    returnMap.put("code",500);
                    returnMap.put("msg","保存失败！");
                }*/
            }
        }catch (Exception e){
            returnMap.put("code",500);
            returnMap.put("msg",e);
        }
        return MapperUtils.mapToJson(returnMap);
    }

    @Anonymous
    @Transactional(rollbackFor = Exception.class)
    @GetMapping("/getProviderClaim")
    public String getOrder() throws Exception {
        HashMap<String,Object> returnMap = new HashMap<>();
        List<ProviderClaim> ProviderClaim = providerClaimService.selectProviderClaimListByProvider();
        if(ProviderClaim!=null&&ProviderClaim.size()>0) {
            returnMap.put("data", ProviderClaim);
            returnMap.put("code", 200);
            returnMap.put("msg", "供应商赔付单获取成功！");
            returnMap.put("success", true);
        }else{
            returnMap.put("data", ProviderClaim);
            returnMap.put("success", true);
            returnMap.put("code", 200);
            returnMap.put("msg", "无信息获取成功！");
        }
        return MapperUtils.mapToJson(returnMap);
    }
   /* @Anonymous
    @RequestMapping("/updateOrderStatus")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/orderList")
    public String updateOrderStatus() throws Exception {
        HashMap<String,Object> returnMap = new HashMap<>();
        try{
            List<Order> orderList = orderService.searchOrderStatus();
            if(orderList!=null && orderList.size()>0)
            {
                returnMap.put("orderList",orderList);
                returnMap.put("code",200);
                returnMap.put("msg","订单状态已更新");
            }
            else
            {
                returnMap.put("orderList","");
                returnMap.put("code",500);
                returnMap.put("msg","未查询到同步数据");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return MapperUtils.mapToJson(returnMap);
    }*/
}
